package com.ideal.lx_mss.service.impl;

import com.google.gson.Gson;
import com.ideal.lx_mss.entity.ContractPurResultEntity;
import com.ideal.lx_mss.mapper.ContractPurResultMapper;
import com.ideal.lx_mss.service.CgService;
import com.ideal.lx_mss.utils.CommonUtil;
import com.ideal.lx_mss.utils.SqlUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

@Service
public class CgServiceImpl implements CgService {

    @Autowired
    private SqlUtils sqlUtils; //数据库工具

    @Autowired
    private ContractPurResultMapper contractPurResultMapper; //合同采购结果信息

    private static final String[] targetTanle = {"contract_pur_result","notice_tender","purchase_case","purchase_case_serial","purchase_result"};

    /**
     * 临时表的创建和清空
     * @param topic
     * @param rcList
     * @param fclList
     * @param ddlList
     * @param dataList
     * @param exceptionList
     * @param tableNames
     */
    @Override
    public void cgDataServe(String topic, List<JSONObject> rcList, List<JSONObject> fclList, List<JSONObject> ddlList, List<JSONObject> dataList, List<String> exceptionList, HashSet<String> tableNames) {
        HashSet<String> tableTempSet = new HashSet<>();
        String tempId = CommonUtil.createTempId();
        //新建临时表
        for (String tableName : targetTanle) {
            if (tableNames.contains(tableName)){
                String tableTemp = tableName+"_"+tempId;
                tableTempSet.add(tableTemp);
//                boolean sqlTable = sqlUtils.createSqlTable(tableName, tableTemp);
            }
        }

        System.out.println("");


//        RListServe(rList);
    }

    /**
     * 全量数据处理
     * @param rList 全量数据
     */
    public void RListServe(List<Map<String, Object>> rList){
        List<Map<String, Object>> exceptionList = new ArrayList<>();
        for (Map<String, Object> map : rList) {
            Gson gson = new Gson();
            //目标表
            JSONObject jsonObject = new JSONObject(map.get("value").toString());
            String datatype = jsonObject.getString("datatype"); //数据类型
            String itfcode = jsonObject.getString("itfcode");//表名

            JSONObject dataload = jsonObject.getJSONObject("dataload");
            try {
                if ("contract_pur_result".equals(itfcode)&&"R".equals(datatype)){
                    //合同采购结果信息 json转对象
                    ContractPurResultEntity contractPurResultEntity = gson.fromJson(dataload.toString(), ContractPurResultEntity.class);
//                    contractPurResultMapper.insertContractPurResultTemp(contractPurResultEntity,"contract_pur_result_temp");

                }
            }catch (Exception e){
                exceptionList.add(map);
            }

        }

    }
}
